<?php
namespace Admin\Controller;
use Think\Controller;
class systemController extends AuthController {

   public function _initialize(){
         
		 parent::_initialize();
    }

    public function index(){
	    $config  = M("config");
		
		//$myfile = fopen("newfile.php", "w") or die("Unable to open file!");
//		
//		fwrite($myfile, $txt);
//		fclose($myfile);
//		
//		//$a = file_get_contents("http://www.sootou.com");
//		$a = fopen("http://www.sootou.com");
//		$a = fgetss($a);
//		print_r($a);
		$data = $config->where('sid=1')->find();
		$data['mail_host']     = C('MAIL_HOST');
		$data['mail_port']     = C('SMTP_PORT');
		$data['mail_smtpauth'] = C('MAIL_SMTPAUTH');
		$data['mail_username'] = C('MAIL_USERNAME');
		$data['mail_password'] = C('MAIL_PASSWORD');
		$data['mail_from']     = C('MAIL_FROM');
		$data['mail_fromname'] = C('MAIL_FROMNAME');
	    $this->assign(array(
		    'data'  => $data,
			'now'   => time(),
		));
	   
        $this->display();
    }
	
	 public function sys_update(){
	    $sid     = I('get.sid');
	    $config  = M("config");
	   if (IS_POST){
	         /*处理图片上传*/
			$upload = new \Think\Upload();// 实例化上传类  
			$upload->maxSize   =    3145728 ;// 设置附件上传大小  
			$upload->exts      =    array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型  
			$upload->savePath  =    '/'; // 设置附件上传目录
			$upload->saveName  =    array('date','YmdHis');
			$upload->subName   =    array('date','Ymd');
			$photo             =   $upload->upload(array($_FILES['photo']));

			$data['webname']     =   I('post.webname','','htmlspecialchars');
			$data['weburl']      =   I('post.weburl','','htmlspecialchars');
			$data['weblogo']     =    $photo ? "/Uploads".$photo[0]['savepath'].$photo['0']['savename'] : I('post.photo1');//防止图片覆盖
			$data['keyword']     =   I('post.keyword','','htmlspecialchars');
			$data['icp']         =   I('post.icp','','htmlspecialchars');
			$data['copyright']   =   I('post.copyright','','htmlspecialchars');
			$data['isopen']      =   I('post.isopen') ? I('post.isopen') : 1;
			$data['limitip']     =   I('post.limitip','','htmlspecialchars');
			$data['maxlogin']    =   I('post.maxlogin') ? I('post.maxlogin') : 5;
			$data['sys_pagenum'] =   I('post.sys_pagenum') ? I('post.sys_pagenum') : 20;
			$data['sys_picsize'] =   I('post.sys_picsize') ? I('post.sys_picsize') : 2;
			$data['sys_attasize']=   I('post.sys_attasize') ? I('post.sys_attasize') : 2;
			$data['isopenad']    =   I('post.isopenad') ? I('post.isopenad') : 0;
			$data['update_time'] =   I('post.update_time') ? strtotime(I('post.update_time')) : time();
			$data['email_test']  =   trim(I('post.mail_test'));
			$data['email_title'] =   trim(I('post.mail_title'));
			$data['email_content']=  trim(I('post.mail_content'));
			
			$data['mail']['sms_model']    =  trim(I('post.sms_model'));
			$data['mail']['mail_host']    =  trim(I('post.mail_host'));
			$data['mail']['mail_port']    =  trim(I('post.mail_port'));
			$data['mail']['mail_from']    =  trim(I('post.mail_from'));
			$data['mail']['mail_fromname']=  trim(I('post.mail_fromname'));
			$data['mail']['mail_smtpauth']=  trim(I('post.mail_smtpauth'));
			$data['mail']['mail_username']=  trim(I('post.mail_username'));
			$data['mail']['mail_password']=  trim(I('post.mail_password'));
			
			$data['mail'] = json_encode($data['mail']);
		}
		if (!isset($sid) || $sid ==""){//id为空表示新增
			 $this->error("传递参数失败");	
	   }else{//修改信息
	        $where['sid'] = $sid;
	        $count = $config->where($where) -> save($data);
			if($count !== false){
			    $this->success("修改成功",U('System/index'));
			}else{
			    $this->error("修改失败");	
			}
	   }
    }
	
	/*
	 *邮件发送测试
	 *sendMail函数
	*/
	public function send_email(){
	    $mail_test    = I("get.mail_test");
		$mail_title   = I("get.mail_title");
		$mail_content = I('get.mail_content','','stripcslashes');
	    $data = "";
		if(sendMail($mail_test,$mail_title,$mail_content)){
            $data = 1;
        }else{
           $data = "";
        }
		$this->ajaxReturn($data);
	}
	
	/*
	* 富文本图片上传
	*/
	public function upload_img(){
	    $data = "";
		 /*处理图片上传*/
		$upload = new \Think\Upload();// 实例化上传类  
		$upload->maxSize   =    3145728 ;// 设置附件上传大小  
		$upload->exts      =    array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型  
		$upload->savePath  =    '/'; // 设置附件上传目录
		$upload->saveName  =    array('date','YmdHis');
		$upload->subName   =    array('date','Ymd');
		$file             =   $upload->upload(array($_FILES['file']));
	    //$_SERVER['SERVER_NAME']//当前网址
		
		$data = __ROOT__."/Uploads".$file[0]['savepath'].$file['0']['savename'];
		//$data ="aaa";
		if($data){
		    $res['code'] = 0;
		    $res['msg'] = "上传成功";
			$res['data']['src'] = $data;
		    $res['data']['title'] ="图片";
		}else{
		    $res['code'] = 1;
		    $res['msg'] = "上传失败";
		}
		
		
		//echo $data;
		$this->ajaxReturn(json_encode($res));
	}
	
	
}